package com.qf.controller.advices;

import com.qf.dto.R;
import com.qf.exceptions.MyBaseException;
import com.qf.exceptions.ResultCode;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author ztf
 * @Date 21:36 2022/1/5
 **/
@ControllerAdvice
@Component
@ResponseBody
public class MyControllerAdvice {

    /**
     * 专门处理MyBaseException类型的异常
     * @param e
     * @return
     */
    @ExceptionHandler(MyBaseException.class)
    public R processMyBaseException(MyBaseException e) {
        e.printStackTrace();
        int code = e.getCode();
        String message = e.getMessage();
        return R.setError(code, message);
    }

    /**
     * 处理未知异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R processMyBaseException(Exception e){
        e.printStackTrace();
        return R.setError(ResultCode.FAIL, "请检查网络",null);
    }


}
